using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace OpenST
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class OpenST : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Composition composition;
        CompositionRenderer compositionVisualiser;
        GUI.Editor editor;
        GUI.BugMixer mixer;
        Tezzanator.Net.XNAGUI.Gui GUI;

        
        public OpenST()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferWidth = 1200;
            graphics.PreferredBackBufferHeight = 700;
            
            Content.RootDirectory = "Content";
            composition = new Composition(this);
            compositionVisualiser = new CompositionRenderer(this);

            GUI = new Tezzanator.Net.XNAGUI.Gui(this);
            this.Components.Add(GUI);

            editor = new GUI.Editor(this, compositionVisualiser);
            mixer = new GUI.BugMixer(this, composition);


            this.IsFixedTimeStep = false;
            this.IsMouseVisible = false;
            //graphics.SynchronizeWithVerticalRetrace = false;
        }


        public static T GetComponent<T>(Game game) where T : GameComponent
        {
            foreach (GameComponent gc in game.Components) // game_object is my static singleton, assigned in the Game constructor
            {
                if (gc.GetType().Equals(typeof(T)))
                {
                     return (T)gc;
                }
            }

            return null;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            compositionVisualiser.SetComposition(composition);
            base.Initialize();
        }


        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            compositionVisualiser.LoadContent(Content);

            editor.LoadContent();
            mixer.LoadContent(Content);

            composition.NewComposition();
        }

        protected override void UnloadContent()
        {
           
        }

        protected override void Update(GameTime gameTime)
        {
            base.Update(gameTime); //Process GUI first

            composition.Update(gameTime);
            compositionVisualiser.Update(gameTime);
            editor.Update(gameTime);
            mixer.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {

            GraphicsDevice.Clear(Color.DarkSlateGray);
            compositionVisualiser.Draw(gameTime);
            editor.Draw(gameTime);

            base.Draw(gameTime);

            if (!false)
            {
                System.Diagnostics.Debug.WriteLine(!false);
            }

            mixer.Draw(gameTime);
        }
    }
}
